﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sebrae.Academico.Trilhas.Dominio.Classes;
using System.Data;
using Sebra.Academico.InfraEstrutura.Core.Helper;
using Sebrae.Academico.Trilhas.BP;

namespace Sebrae.Academico.Trilhas.WebForms.Cadastros
{
    public partial class ListaTrilha : System.Web.UI.Page
    {
        DataTable dt = new DataTable();
        ManterTrilha manterTrilha = new ManterTrilha();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            DataTable dt = new DataTable();
            //dt = DataHelper.ConvertToDataTable(manterTrilha.ObterTrilhas());
            var trilhas = manterTrilha.ObterTrilhas().OrderBy(x => x.Nome).ToList<Trilha>();
            dgTrilha.DataSource = trilhas;
            //dgTrilha.DataMember = dt.TableName;
            dgTrilha.DataBind();

        }

        protected void dgTrilha_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                Session.Add("TrilhaEdit", ((GridView)e.CommandSource).SelectedRow.Cells[2]);
                Response.Redirect("EdicaoTrilha", false);
            }
        }

        protected void btnPesquisa_Click(object sender, EventArgs e)
        {
            dgTrilha.DataSource = (IList<Trilha>)manterTrilha.ObterTrilhaPorNome(txtFiltroNome.Text).OrderBy(x => x.Nome).ToList();
            dgTrilha.DataBind();
        }

    

        protected void dgTrilha_SelectedIndexChanged(object sender, EventArgs e)
        {

        
            int id = (int)dgTrilha.DataKeys[dgTrilha.SelectedIndex].Value;
            Session.Add("TrilhaEdit", id);
            Response.Redirect("EdicaoTrilha.aspx", false);
        }

        protected void dgTrilha_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            manterTrilha.ExcluirTrilha(new Trilha() { ID = int.Parse(e.Keys[0].ToString()) });
            Response.Redirect("ListaTrilha.aspx");
        }


        protected void btnNovo_Click(object sender, EventArgs e)
        {
            Response.Redirect("EdicaoTrilha.aspx");
        }

        protected void dgTrilha_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dtsort = DataHelper.OrdenarLista((DataTable)Session["listaGrid"], e.SortExpression);
            dgTrilha.DataSource = dtsort;
            dgTrilha.DataBind();
            Session.Add("listaGrid", dtsort.Copy());
        }

        

       
       

     

        

       
    }
}